import gen.GoLexer;
import gen.GoParser;
import org.antlr.v4.runtime.ANTLRInputStream;
import org.antlr.v4.runtime.CharStream;
import org.antlr.v4.runtime.CommonTokenStream;
import org.antlr.v4.runtime.tree.ParseTree;
import org.antlr.v4.runtime.tree.ParseTreeWalker;
import symbolTable.SymbolTable;
import threeAddressGenerator.ThreeAddressGenerator;

import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;

public class test {
    public static void main(String[] args) throws IOException {
        String inputFile = null;
        if (args.length > 0) inputFile = args[0];
        InputStream is = System.in;
        if (inputFile != null) is = new FileInputStream(inputFile);
        CharStream stream = (CharStream) new ANTLRInputStream(is);
        GoLexer lexer = new GoLexer(stream);
        CommonTokenStream tokens = new CommonTokenStream(lexer);
        GoParser parser = new GoParser(tokens);
        parser.setBuildParseTree(true);
        ParseTree tree = parser.sourceFile();

        ParseTreeWalker walker = new ParseTreeWalker();
        SymbolTable symbolTable = new SymbolTable();
        walker.walk(symbolTable, tree);
//        TypeChecker typeChecker = new TypeChecker(symbolTable.getSymbols(), symbolTable.getScopes(), symbolTable.getGlobalScope());
//        walker.walk(typeChecker, tree);
        ThreeAddressGenerator threeAddressGenerator = new ThreeAddressGenerator(symbolTable.getSymbols());
        walker.walk(threeAddressGenerator, tree);
        threeAddressGenerator.writeThreeAddressToFile("D:\\Projects\\JavaProjects\\compile_practice\\Go-compile\\asset\\result.txt");
    }
}
